﻿//----------------------------------------------------------------
// Copyright (C) 2015-2020 PeachSystem Co.,Ltd.
// All rights reserved.
//
// 文件:EnityHelper.cs
// 摘要:消息实体工具类
// 说明:
//

//当前版本：1.0.0
//作    者：陶剑扬
//完成日期：2014-11-14
//----------------------------------------------------------------

using System;
using System.Xml;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Reflection;
using System.Collections;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace PeachSystem.XWX.Helpers
{
    /// <summary>
    /// 消息实体工具类
    /// </summary>
    public static class EnityHelper
    {
        public static T GetEntity<T>(string xml)
        {
            //T t = new <T>();
            return default(T);
        }

        public static void FillEntity<T>(T e, XDocument xdoc) where T : class, new ()
        {
            e = e ?? new T();
            XElement root = xdoc.Root;
            PropertyInfo[] props = e.GetType().GetProperties();

            string pname;
            XElement xe;
            foreach (PropertyInfo p in props)
            {
                pname = p.Name;
                xe = root.Element(pname);
                if (xe != null)
                {
                    switch (p.PropertyType.Name)
                    {
                        case "Int32":
                            p.SetValue(e, int.Parse(xe.Value));
                            break;
                        case "Int64":
                            p.SetValue(e, long.Parse(xe.Value));
                            break;
                        case "Boolean":
                            p.SetValue(e, xe.Value == "1");
                            break;
                        case "DateTime":
                            p.SetValue(e, Utility.ConvertToDateTime(double.Parse(xe.Value)));
                            break;
                        default:
                            p.SetValue(e, xe.Value);
                            break;
                    }
                }
            }
        }
    }
}
